package com.cloudptt.wp.ui.more;

import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.SysOperator;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.mcptt.config.local.SysConfig;

import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

public class VideoSettingActivity extends PreferenceActivity implements OnPreferenceChangeListener, OnClickListener {

	public static String TAG = "VideoSettingActivity";
	
	private SysConfig config = null;

	private ListPreference resolvingPref;

	private String currentResolving;

	private String[] resolvingList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		Window window = getWindow();
		window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
		window.setStatusBarColor(getResources().getColor(R.color.app_theme_color_primary));
		super.onCreate(savedInstanceState);
		getActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.app_theme_color_primary));
		getActionBar().hide();
		
		setContentView(R.layout.video_setting);
		ImageView iv = (ImageView) findViewById(R.id.back_up_icon);
		iv.setOnClickListener(this);
		TextView tv = (TextView) findViewById(R.id.title);
		tv.setText(R.string.video_setting);

		addPreferencesFromResource(R.xml.video_settings);

		resolvingPref = (ListPreference) findPreference("resolving_key");

		resolvingPref.setOnPreferenceChangeListener(this);

		initData();
	}

	public void initData(){
		SysApplication app = ((SysApplication) getApplicationContext());
		Product product = app.getProduct();
		config = product.getSysConfig();

		if(config != null)
		{
			currentResolving = config.getVideoConfig().getVideoResolvingDesc();
			resolvingList = config.getVideoConfig().getVideoResolvingArr();
			resolvingPref.setEntries(resolvingList);
			Log.d(TAG, "currentResolving = " + currentResolving);
			Log.d(TAG, "resolvingList = " + resolvingList);
		}
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		updatePref();
		resolvingPref.setSummary(currentResolving);
	}
	
	private void updatePref(){
		int index = -1;
		for(int i = 0; i < resolvingList.length; i++){
			if(currentResolving.equals(resolvingList[i])){
				index = i;
				resolvingPref.setValueIndex(i);
			}
		}
	}

	public void updateSummary(int index){
		resolvingPref.setSummary(resolvingList[index]);
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		// TODO Auto-generated method stub
		int value = Integer.valueOf((String)newValue);
		if(config != null)
		{
			config.setVideoResolving(value);

			SysApplication app = ((SysApplication) getApplicationContext());
			Product product = app.getProduct();
			SysOperator operator = (SysOperator)product.getOperator(ProductConst.Func_Sys);
			if(operator != null)
			{
				operator.setVideoAttr(value);
				updateSummary(value);
				return true;
			}
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		if(id == R.id.back_up_icon) 
		{
			this.finish();
		}
	}
}
